// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import VueAxios from 'vue-axios'
import VueAuth from '@websanova/vue-auth'

import VueRouter from 'vue-router';
import iView from 'iview';

import Vuex from 'vuex';
import store from './vuex/store';

import 'iview/dist/styles/iview.css';    // 使用 CSS
import 'animate.css/animate.css'
Vue.use(VueRouter);
Vue.use(iView);
Vue.use(VueAxios, axios);
Vue.use(Vuex);
Vue.axios.defaults.baseURL = 'http://youzhubuget-demo.aliyun-c1.centling.cn/youzhubuget-test/';
Vue.config.productionTip = false
Vue.router = router;

// Vue Auth
Vue.use(VueAuth, {
  auth: require('../drivers/auth/bearer.js'),
  http: require('@websanova/vue-auth/drivers/http/axios.1.x.js'),
  router: require('@websanova/vue-auth/drivers/router/vue-router.2.x.js'),
  rolesVar: "authorities",
  token: [{request: 'Authorization', response: 'Authorization', authType: 'bearer', foundIn: 'header'}],
  loginData: {url: 'api/authenticate', method: 'POST', redirect: '/', fetchUser: true},
  fetchData: {url: 'api/account', method: 'GET', enabled: true},
  refreshData: {url: 'auth/refresh', method: 'GET', enabled: false, interval: 30},
  logoutData: {url: 'auth/logout', method: 'POST', redirect: '/login', makeRequest: false},
  parseUserData: function (data) {
    return data;
  }
});


/* eslint-disable no-new */
const vm = new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  created: function () {
    this.$auth.fetch({
      success() {
        this.$router.push({ path: '/' });
      },
      error() {
        this.$router.push({ path: '/login' });
      }
    })
  },
  mounted: function () {
    router.beforeEach(({meta, path}, from, next) => {
      const {auth = true} = meta      // meta代表的是to中的meta对象，path代表的是to中的path对象
      var isLogin = Boolean(this.$auth.check())    // true用户已登录， false用户未登录　
      console.log(this.$auth.check())
      if (auth  &&  !isLogin  &&  path !== '/login') {   // auth 代表需要通过用户身份验证，默认为true，代表需要被验证， false为不用检验
        return next({ path: '/login' })   //  跳转到login页面
      }
      next()   // 进行下一个钩子函数
    })
  },
  components: { App }
})

